package com.ijiuchuang.jc_localization.view.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.ijiuchuang.jc_localization.R;
import com.ijiuchuang.jc_localization.bean.bmob.Banner;
import com.ijiuchuang.jc_localization.bean.api.Keji;
import com.ijiuchuang.jc_localization.contract.HomeContract;
import com.ijiuchuang.jc_localization.presenter.HomePresenter;
import com.ijiuchuang.jc_localization.tools.adapter.recyclerview.DividerItemDecoration;
import com.ijiuchuang.jc_localization.view.adapter.HomeBannerAdapter;
import com.ijiuchuang.jc_localization.view.adapter.TrendsListRecyclerAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment implements HomeContract.View {

    @BindView(R.id.home_convenient_banner)
    ConvenientBanner mConvenientBanner;
    @BindView(R.id.home_recycler_trends)
    RecyclerView mRecyclerView;

    private TrendsListRecyclerAdapter mRecyclerAdapter;

    private HomeContract.Presenter presenter;

    public HomeFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        ButterKnife.bind(this, view);

        presenter = new HomePresenter(getActivity(), this);

        return view;
    }

    @Override
    public void setPresenter(HomeContract.Presenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void setBanners(List<Banner> list) {
        //网络加载例子
        mConvenientBanner.setPages(new CBViewHolderCreator<HomeBannerAdapter>() {
                    @Override
                    public HomeBannerAdapter createHolder() {
                        return new HomeBannerAdapter();
                    }
                }, list)
                .setPageIndicator(new int[]{R.drawable.ic_page_indicator, R.drawable.ic_page_indicator_focused})
                .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.ALIGN_PARENT_RIGHT);
    }

    @Override
    public void setTrends(Keji list) {
        mRecyclerAdapter = new TrendsListRecyclerAdapter(getActivity(), R.layout.recycler_item_keji, list.getNewslist());
        // 屏蔽滑动事件 ---> ScrollView 嵌套 RecyclerView 导致 后者滑动不流畅，无法惯性滑动
        mRecyclerView.setNestedScrollingEnabled(false);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
        mRecyclerView.setAdapter(mRecyclerAdapter);
    }

    @Override
    public void onStart() {
        super.onStart();

        presenter.start();
    }

    @Override
    public void onResume() {
        super.onResume();

        //开始自动翻页
        mConvenientBanner.startTurning(5000);
    }

    @Override
    public void onStop() {
        super.onStop();

        //停止翻页
        mConvenientBanner.stopTurning();
    }
}
